<!DOCTYPE html>
<html><head>
<script src="io.js"></script>
<script src="archive.js"></script>
</head>
<body>
  <input id='filechooser' type='file'></input>
</body>
<script>

  function assertTrue(a) {
  	if (!a) {
  	  alert("'" + a + "' not true");
  	}
  }
  function assertEquals(a,b) {
  	if (a != b) {
  	  alert("'" + a + "' != '" + b + "'")
  	}
  }

  function testAddRemoveEventListeners() {
    var progress = function(e) { alert('progress') };
    var progress2 = function(e) { alert('progress2') };

    var ua = new bitjs.archive.Unarchiver(null);
    ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
    ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
    ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
    ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress2);
    ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress2);
    assertEquals(2, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);
  
    ua.removeEventListener(bitjs.archive.UnarchiveEvent.Type.START, progress);
    assertEquals(2, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);

    ua.removeEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, progress);
    assertEquals(1, ua.listeners_[bitjs.archive.UnarchiveEvent.Type.PROGRESS].length);
  }

  function testAbstractUnarchiveThrows() {
    var ua = new bitjs.archive.Unarchiver(null);
    try {
      ua.start();
      alert("unarchive() didn't throw");
    } catch(e) {}
  }

// unit tests
//  testAddRemoveEventListeners();
//  testAbstractUnarchiveThrows();
//  var uz = new bitjs.archive.Unzipper(null);

  document.body.querySelector("#filechooser").addEventListener("change",
      function(evt) {
        var inp = evt.target;
        var filelist = inp.files;
        if (filelist.length == 1) {
          var blob = filelist[0];
          var fr = new FileReader();
          fr.onload = function() {
            var ua = new bitjs.archive.Unzipper(fr.result);
            ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.INFO, function(e) {
              console.log("info: " + e.msg);
            })
            ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, function(e) {
              console.log("progress: " + e.msg);
            })
            ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT, function(e) {
              console.log("extract: " + e.unarchivedFile.filename);
            })
            ua.start();
          };
          fr.readAsArrayBuffer(blob);
        }
      }, false);

</script>
</html>